<?php

// +-----------------------------------------------------------------------
// | @Copyright (c) 2012 http://t00ls.net.
// +-----------------------------------------------------------------------
// | @Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +-----------------------------------------------------------------------
// | @author: lenush <jnicklasj@gmail.com> qq:707207845
// +-----------------------------------------------------------------------

final class Gos_Controller_Default extends Gospel_Controller_Api {

    public function unitTest() {
        #echo 'phpunit testing!';
    }

    public function index() {
//        $module = new Gos_Model_Category();
//        $where = 'id=1';
//        $category = $module->getItems($where, 'id');
//        return $category;
        $this->tpl->draw('default/index');
    }

    public function getCategory() {
        $module = new Gos_Model_Category();
        $where = 'id=1';
        $category = $module->getItems($where, 'id');
        return $category;
    }

    public function get_the_istop() {
        #$istop = array();
        return 2;
        #return $category();
    }

    public function dologin() {
        $return = true;
        $where = sprintf("login='%s' and passwd='%s'", $this->getRequest()->getPost('login'), md5(strtolower($this->getRequest()->getPost('passwd'))));
        try {
            $module = new Gos_Model_Account();
            if ($result = $module->getItem($where)) {
                $loginAcc['loginid'] = $result['id'];
                $loginAcc['nick'] = $result['nick'];
                Gospel_Http_Session::start();
                session_regenerate_id(); //解决session固定会话攻击
                Gospel_Http_Session::set('loginAcc', $loginAcc);
                $this->message('登录成功,3秒后转向操作页面!', '/Student/index');
            } else {
                $this->message('登录失败,3秒后转向登录页面!', '/Default/index');
            }
        } catch (Gospel_Db_Exception $mde) {
            throw $mde;
        }
    }

    public function logout() {
        $return = true;
        Gospel_Http_Session::start();
        $loginAcc = Gospel_Http_Session::get('loginAcc');
        $array['lastlogin'] = time();
        $array['lastip'] = $this->getRequest()->getClientIp();
        try {
            $where = sprintf("id='%d'", $loginAcc['loginid']);
            $module = new Gos_Model_Account();
            if (false == $module->alter($where, $array)) {
                $return = false;
            }
        } catch (Gospel_Db_Exception $mde) {
            throw $mde;
        }
        Gospel_Http_Session::destory();
        header("Location:/Default/index", true, 301);
    }
    
    public function user(){
        $module = new Gos_Model_Account();
        $where = sprintf("id='%d'", $this->getRequest()->getInt('parameters'));
        $employee = $module->getItem($where);
        $this->tpl->assign('employee',$employee);
        $this->tpl->draw('default/user');
    }

}

/**
* // +---------------------------------------------------------------------
* // | @ Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
* // +---------------------------------------------------------------------
* // | @ author: lenush <jnicklasj@gmail.com> qq:707207845
* // +---------------------------------------------------------------------
* Local variables:
* tab-width:4
* basic-offset:4
* indent-tabs-mode:t
* End:
*/
